﻿Public Class FViewSharedItemDetails_Remote
    Private Shared mActiveForms As New Generic.Dictionary(Of String, FViewSharedItemDetails_Remote)

    Public Shared Shadows Sub Show(ByVal owner As IWin32Window, ByVal client As NetNode.Client, ByVal item As Entities.SharedItem)
        Dim strKey As String = client.Buddy.Id.ToString & "_" & item.Id.ToString
        If mActiveForms.ContainsKey(strKey) Then
            mActiveForms(strKey).Activate()
        Else
            Dim clsView As New FViewSharedItemDetails_Remote
            clsView.ShowIt(strKey, client, item)
            mActiveForms.Add(strKey, clsView)
        End If
    End Sub

    Public Shared Sub CloseAll()
        For i As Integer = mActiveForms.Count - 1 To 0 Step -1
            mActiveForms.Values(i).Close()
        Next
    End Sub

    Private mSharedItem As Entities.SharedItem
    Private mClient As NetNode.Client
    Private mFormKey As String
    Private mWaitingForMessageIdEx As Integer

    Private Sub ShowIt(ByVal formKey As String, ByVal client As NetNode.Client, ByVal item As Entities.SharedItem)
        mFormKey = formKey
        mSharedItem = item
        mClient = client

        Me.Text = String.Format("{0} - {1} - Shared Item Details", item.Name, client.Buddy.Name)
        Select Case item.ItemType
            Case Entities.ItemType.Movies
                Me.Icon = My.Resources.Movie
            Case Entities.ItemType.TVSeries
                Me.Icon = My.Resources.TV
            Case Else
                Me.Icon = CommonResources.My.Resources.Buddy
        End Select

        SharedItemMediaInfo1.UpdateView(Nothing, Nothing, Nothing)
        StartGetDetails(item)

        SetupEvents(True)
        MyBase.Show()
    End Sub

    Private Sub StartGetDetails(ByVal item As Entities.SharedItem)
        SharedItemMediaInfo1.Enabled = False
        mWaitingForMessageIdEx = FileShare.Manager.StartGetSharedItemDetails(mClient, item.Id)
    End Sub

    Private Sub FViewSharedItems_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        mActiveForms.Remove(mFormKey)
        SetupEvents(False)
    End Sub
    Private Sub SetupEvents(ByVal startListening As Boolean)
        If startListening Then
            AddHandler Manager.GetSharedItemDetailsReply, AddressOf EventHandler_GetSharedItemDetailsReply
        Else
            RemoveHandler Manager.GetSharedItemDetailsReply, AddressOf EventHandler_GetSharedItemDetailsReply
        End If
    End Sub

    Private Sub EventHandler_GetSharedItemDetailsReply(ByVal client As NetNode.Client, ByVal reply As FileShare.Commands.GetSharedItemDetails.Reply)
        Try
            If reply.InReplyToMessageId = mWaitingForMessageIdEx Then
                SharedItemMediaInfo1.Enabled = True
                SharedItemMediaInfo1.UpdateView(reply.Item, reply.Files, reply.MediaInfo)
            End If
        Catch ex As Exception
            Utils.Misc.HandleException(Me, ex)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub
End Class